/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.GraphEdgeHelper;
import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

// TODO: Auto-generated Javadoc
/**
 * The Class EdgeProfileApplicationHelper.
 */
public class EdgeProfileApplicationHelper extends GraphEdgeHelper implements IGraphEdgeHelper{

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * Can connect.
	 * 
	 * @param target the target
	 * @param source the source
	 * 
	 * @return true, if can connect
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		if( source!=null&& (source instanceof Package)&& target==null){
			return true;
		}
			if ((source instanceof Package) && (target instanceof Profile)){// they are  classifiers
					if (source==target){return false;}// generalization not on the same object
					return true; // attention Detection of loops
				}
		return false;

	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * Can create graph element.
	 * 
	 * @param source the source
	 * 
	 * @return true, if can create graph element
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;
		if ((source instanceof Diagram)  ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if (eltSource instanceof Package){return true;}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof Package){// they are  classifiers
					return true;
				}

			}
		}
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#createGraphEdge(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * Creates the graph edge.
	 * 
	 * @param umlElement the uml element
	 * @param diagram the diagram
	 * 
	 * @return the array list
	 */
	@Override
	public ArrayList createGraphEdge(Element umlElement, Diagram diagram){
		Debug.debug(this, "createGraphEdge", 10);
		
		GraphEdge ge = (GraphEdge)this.createGraphElement(umlElement, null,null,null);		
		//look for UML connected UML element
		if (!(umlElement instanceof ProfileApplication)) {
			Debug.error(this, "EdgeProfileApplicationHelper.createGraphEdge: "+ umlElement+ "is not a profile application!", channel);
		}
		
		ProfileApplication de = (ProfileApplication) umlElement;
		
		Element umlsource = de.getApplyingPackage();
		Element umltarget = de.getAppliedProfile();
		super.endCreateGraphElement(umlsource, umltarget, ge, diagram);
		return new ArrayList();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createGraphNodeWithSemantic(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Creates the graph element.
	 * 
	 * @param umlChild the uml child
	 * @param size the size
	 * @param location the location
	 * @param parent the parent
	 * 
	 * @return the graph element
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {
	
		GraphElement ge=super.createGraphElement(umlChild, null, location, size);
		NodeHelper nf=new NodeHelper();
//		stereotype of the Generalization
		GraphNode graphNode=nf.createGraphElement(null,ge,new Point(0, 30),null);
		graphNode.setFontSize(9);
		
//		the parent is connected in last, because an eventis triggered.
		if( parent!=null){
			parent.getContained().add(ge);
		}
		return ge;	
	}
	
	///////////////////////////////////////////////////////////////////////////
	// UML Management
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Creates the UML element.
	 * 
	 * @param umlParent the uml parent
	 * @param name the name
	 * 
	 * @return the element
	 */
	public org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create ProfileApplication", channel); //$NON-NLS-1$
		ProfileApplication pi = UMLFactory.eINSTANCE.createProfileApplication();
		((Package)umlParent).getProfileApplications().add(pi);
		return pi;		
	}


	/**
	 * Can create UML element.
	 * 
	 * @param umlParent the uml parent
	 * 
	 * @return true, if can create UML element
	 */
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof Package) {
			return true;
		}
		return false;
	}

	
	/**
	 * Adds the UML element.
	 * 
	 * @param element the element
	 * @param umlParent the uml parent
	 * 
	 * @return true, if add UML element
	 */
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getProfileApplications().add((ProfileApplication)element);
		}
		return false;
	}

	
	/**
	 * Removes the UML element.
	 * 
	 * @param element the element
	 * @param umlParent the uml parent
	 * 
	 * @return true, if remove UML element
	 */
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getProfileApplications().remove(element);
		}
		return false;
	}

	/**
	 * Connect.
	 * 
	 * @param target the target
	 * @param link the link
	 * @param source the source
	 * 
	 * @return true, if connect
	 */
	public boolean connect(Element source, Element target, Element link) {
		ProfileApplication gen =(ProfileApplication)link;
		// this is the getSource() that contains the Generalization
		Package cls= (Package)source;
		gen.setAppliedProfile((Profile)target);
		cls.getProfileApplications().add(gen);
		return true;
	}
	
	/**
	 * Deconnect.
	 * 
	 * @param target the target
	 * @param link the link
	 * @param source the source
	 * 
	 * @return true, if deconnect
	 */
	public boolean deconnect(Element source, Element target, Element link) {
		ProfileApplication gen =(ProfileApplication)link;
		// this is the getSource() that contains the Generalization
		Package cls= (Package)source;
		gen.setAppliedProfile(null);
		cls.getProfileApplications().remove(gen);
		return true;
	}
	
	/**
	 * Gets the base string.
	 * 
	 * @return the base string
	 */
	public String getBaseString() {
		return "ProfileApplication"; //$NON-NLS-1$
	}
}

